﻿using DSPL.Contracts;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DSPL.Contracts.Common;

namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for PurchaseDataGrid.xaml
    /// </summary>
    public partial class PurchaseDataGrid : UserControl
    {
        private ObservableCollection<DataGridColumn> currentColumns;

        public PurchaseDataGrid()
        {
            InitializeComponent();
            currentColumns = this.CustomColumnsGrid.Columns;
        }
                
        #region Declaring Properties

        #region Private Variables

        private ObservableCollection<DataGridColumn> columns;
        private bool isProductCodeFocus;
        private int itemRowIndex;

        #endregion

        #region Public Properties/ Variables

        public ObservableCollection<DataGridColumn> Columns
        {
            get
            {
                return columns;
            }
            set
            {
                columns = value;

                SetColumns();
            }
        }


        public bool IsProductCodeFocus
        {
            get { return isProductCodeFocus; }
            set { isProductCodeFocus = value; }
        }
        
        public int ItemRowIndex
        {
            get { return itemRowIndex; }
            set { itemRowIndex = value; }
        }
        
        public Modes FormMode
        {
            get { return (Modes)GetValue(FormModeProperty); }
            set { SetValue(FormModeProperty, value); }
        }

        public DataTable DtChildTransaction
        {
            get { return (DataTable)GetValue(DtChildTransactionProperty); }
            set { SetValue(DtChildTransactionProperty, value); }
        }

        public int ProductCodeIndex
        {
            get { return (int)GetValue(ProductCodeIndexProperty); }
            set { SetValue(ProductCodeIndexProperty, value); }
        }

        public int ProductDescriptionIndex
        {
            get { return (int)GetValue(ProductDescriptionIndexProperty); }
            set { SetValue(ProductDescriptionIndexProperty, value); }
        }

        public int QtyIndex
        {
            get { return (int)GetValue(QtyIndexProperty); }
            set { SetValue(QtyIndexProperty, value); }
        }

        public int PurPriceIndex
        {
            get { return (int)GetValue(PurPriceIndexProperty); }
            set { SetValue(PurPriceIndexProperty, value); }
        }

        public int DiscountIndex
        {
            get { return (int)GetValue(DiscountIndexProperty); }
            set { SetValue(DiscountIndexProperty, value); }
        }

        public int ValueIndex
        {
            get { return (int)GetValue(ValueIndexProperty); }
            set { SetValue(ValueIndexProperty, value); }
        }

        public int TaxComIndex
        {
            get { return (int)GetValue(TaxComIndexProperty); }
            set { SetValue(TaxComIndexProperty, value); }
        }

        public string QtyFormat
        {
            get { return (string)GetValue(QtyFormatProperty); }
            set { SetValue(QtyFormatProperty, value); }
        }

        public string AverageRateFormat
        {
            get { return (string)GetValue(AverageRateFormatProperty); }
            set { SetValue(AverageRateFormatProperty, value); }
        }

        public string DiscountPerFormat
        {
            get { return (string)GetValue(DiscountPerFormatProperty); }
            set { SetValue(DiscountPerFormatProperty, value); }
        }

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty DiscountPerFormatProperty = DependencyProperty.Register("DiscountPerFormat", typeof(string), typeof(PurchaseDataGrid), new PropertyMetadata(GlobalVariables.DiscountPerFormat));

        public static readonly DependencyProperty AverageRateFormatProperty = DependencyProperty.Register("AverageRateFormat", typeof(string), typeof(PurchaseDataGrid), new PropertyMetadata(GlobalVariables.AverageRateFormat));

        public static readonly DependencyProperty QtyFormatProperty = DependencyProperty.Register("QtyFormat", typeof(string), typeof(PurchaseDataGrid), new PropertyMetadata(GlobalVariables.QtyFormat));

        public static readonly DependencyProperty FormModeProperty = DependencyProperty.Register("FormMode", typeof(Modes), typeof(PurchaseDataGrid), new PropertyMetadata(Modes.None));

        public static readonly DependencyProperty DtChildTransactionProperty = DependencyProperty.Register("DtChildTransaction", typeof(DataTable), typeof(PurchaseDataGrid), new PropertyMetadata(null));

        public static readonly DependencyProperty ProductCodeIndexProperty = DependencyProperty.Register("ProductCodeIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(0));

        public static readonly DependencyProperty ProductDescriptionIndexProperty = DependencyProperty.Register("ProductDescriptionIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(1));

        public static readonly DependencyProperty QtyIndexProperty = DependencyProperty.Register("QtyIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(2));

        public static readonly DependencyProperty PurPriceIndexProperty = DependencyProperty.Register("PurPriceIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(3));

        public static readonly DependencyProperty DiscountIndexProperty = DependencyProperty.Register("DiscountIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(4));

        public static readonly DependencyProperty ValueIndexProperty = DependencyProperty.Register("ValueIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(5));

        public static readonly DependencyProperty TaxComIndexProperty = DependencyProperty.Register("TaxComIndex", typeof(int), typeof(PurchaseDataGrid), new PropertyMetadata(6));
        
        #endregion

        #endregion

        #region User Control's Internal Events

        void PurchaseDataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            currentColumns = this.CustomColumnsGrid.Columns;
        }

        #endregion

        #region Private Methods

        private void SetColumns()
        {
            if (this.CustomColumnsGrid.Columns != null)
            {
                this.CustomColumnsGrid.Columns.Clear();
            }

            foreach (DataGridColumn item in this.currentColumns)
            {
                this.CustomColumnsGrid.Columns.Add(item);
            }

            foreach (DataGridColumn item in this.columns)
            {
                this.CustomColumnsGrid.Columns.Add(item);
            }
        }

        #endregion
    }
}
